package com.haochang.chunkvod.controller.adapter.order;

import java.util.ArrayList;
import java.util.List;

import com.haochang.base.BaseTextView;
import com.haochang.base.OnBaseClickListener;
import com.haochang.base.OnBaseLongClickListener;
import com.haochang.chunkvod.R;
import com.haochang.chunkvod.app.common.intent.IntentCode;
import com.haochang.chunkvod.app.common.intent.IntentKey;
import com.haochang.chunkvod.app.utils.CollectionUtils;
import com.haochang.chunkvod.app.utils.TimeUtils;
import com.haochang.chunkvod.app.utils.ToastUtils;
import com.haochang.chunkvod.app.utils.VodDialog;
import com.haochang.chunkvod.controller.activity.hostess.ConsumeInfoActivity;
import com.haochang.chunkvod.controller.activity.order.OrderHomePageActivity;
import com.haochang.chunkvod.controller.activity.order.OrderMainActivity;
import com.haochang.chunkvod.controller.adapter.base.BaseAdapterHelper;
import com.haochang.chunkvod.controller.adapter.base.QuickAdapter;
import com.haochang.chunkvod.model.BaseInfo;
import com.haochang.chunkvod.model.order.RoomInfo;
import com.haochang.chunkvod.tools.vod.other.DrawableUtils;
import com.haochang.chunkvod.view.vod.activity.CustomDialogActivity;
import com.haochang.chunkvod.view.vod.activity.SingleInputActivity;

import android.content.Context;
import android.content.Intent;
import android.text.TextUtils;
import android.util.SparseArray;
import android.view.View;
import android.view.animation.AlphaAnimation;
import android.view.animation.Animation;
import android.view.animation.LinearInterpolator;

/**
 * 点餐首页适配器
 */
public class OrderHomePageAdapter extends QuickAdapter
{
	private Context mContext;
	private List<BaseInfo> baseInfoList;
	private SparseArray<View> sparseArray = new SparseArray<View>();//保存具有闪烁功能的view的引用
	private Animation alphaAnimation;//透明度动画,全局变量可以保证动画同步

	public OrderHomePageAdapter(Context context, int layoutResId, List data)
	{
		super(context, layoutResId, data);
		mContext = context;
	}

	public void setBaseInfoList(List<BaseInfo> baseInfoList)
	{
		this.baseInfoList = baseInfoList;
	}

	@Override
	protected void convert(BaseAdapterHelper helper, final Object object)
	{
		final RoomInfo item = (RoomInfo) object;
		final BaseTextView tv_roomStatus = helper.getView(R.id.tv_roomStatus);
		helper.setText(R.id.tv_room_type_count, item.getRoomType() + "(" + item.getRoomPeopleCount() + ")");
		helper.setText(R.id.tv_roomNum, item.getRoomNum());
		helper.setText(R.id.tv_remark, item.getRoomRemark());
		if (TextUtils.isEmpty(item.getStart_time()))
			helper.setText(R.id.tv_time, TimeUtils.getMinutesString(item.getEnd_time()));
		else if (TextUtils.isEmpty(item.getEnd_time()))
			helper.setText(R.id.tv_time, TimeUtils.getMinutesString(item.getStart_time()));
		else
			helper.setText(R.id.tv_time, TimeUtils.getMinutesString(item.getStart_time()) + "-" + TimeUtils.getMinutesString(item.getEnd_time()));
		if (item.getOrderNum() > 0)
			helper.getView(R.id.img_shopping).setVisibility(View.VISIBLE);
		else
			helper.getView(R.id.img_shopping).setVisibility(View.GONE);
		if (item.getType() == 0)
		{
			helper.getView(R.id.tv_batch_num).setVisibility(View.GONE);//隐藏批次号
			if (CollectionUtils.isEmpty(baseInfoList))
				DrawableUtils.setDefaultRoomStyle(mContext, item.getRoomStatus(), item.getUsage(), helper.getView(), tv_roomStatus);
			else
				DrawableUtils.setRoomStyleByService(mContext, item.getRoomStatus(), item.getUsage(), helper.getView(), tv_roomStatus, baseInfoList);
		} else
		{
			if (item.getAbs_number() == 0)
			{
				helper.getView(R.id.tv_batch_num).setVisibility(View.GONE);
			} else
			{
				helper.getView(R.id.tv_batch_num).setVisibility(View.GONE);
				helper.setText(R.id.tv_batch_num, "(+" + item.getAbs_number() + ")");
			}
			if (CollectionUtils.isEmpty(baseInfoList))
				DrawableUtils.settDefaultDeskStyle(mContext, item.getRoomStatus(), helper.getView(), tv_roomStatus);
			else
				DrawableUtils.setDeskStyleByService(mContext, item.getRoomStatus(), helper.getView(), tv_roomStatus, baseInfoList);
		}
		helper.getView(R.id.list_layout).setTag(R.id.order_home_page_list, item);
		helper.getView(R.id.list_layout).setOnClickListener(onBaseClickListener);
		helper.getView(R.id.list_layout).setOnLongClickListener(onBaseLongClickListener);
		//闪烁动画
		if (item.isFlick())
		{
			sparseArray.put(helper.getPosition(), helper.getView());
			startFlick(helper.getView());
		} else
			stopFlick(helper.getView());
	}

	private OnBaseClickListener onBaseClickListener = new OnBaseClickListener()
	{
		@Override
		public void onBaseClick(View v)
		{
			switch (v.getId())
			{
			case R.id.list_layout:
				RoomInfo item = (RoomInfo) v.getTag(R.id.order_home_page_list);
				if (item.getType() == 0)
				{
					if (!TextUtils.isEmpty(item.getBillId()) && !TextUtils.isEmpty(item.getBillStatus())
							&& (item.getBillStatus().equals("unpaied") || item.getBillStatus().equals("paied")))
					{
						onOrderClick(item);
					} else
						ToastUtils.makeText(mContext, R.string.no_order, ToastUtils.TOAST_TIME).show();
				} else
				{
					if (TextUtils.equals(item.getRoomStatus(), "paying"))//桌台状态为买单中
						ToastUtils.makeText(mContext, R.string.no_order_desk, ToastUtils.TOAST_TIME).show();
					else
						onOrderClick(item);
				}
				break;
			default:
				break;
			}
		}
	};

	private OnBaseLongClickListener onBaseLongClickListener = new OnBaseLongClickListener()
	{
		@Override
		public boolean onBaseLongClick(View v)
		{
			final RoomInfo item = (RoomInfo) v.getTag(R.id.order_home_page_list);
			List<String> mMoreOptionItemList = new ArrayList<String>();
			mMoreOptionItemList.add("备注");
			mMoreOptionItemList.add("修改人数");
			new VodDialog.Builder(mContext).dialogEnum(VodDialog.DialogEnum.LIST).ListTitleName(item.getRoomNum())
					.onDialogListListener(new VodDialog.Builder.OnDialogListListener()
					{
						@Override
						public void onSelectItem(int position)
						{
							switch (position)
							{
							case 1:
								onRemarkClick(item);
								break;
							case 2:
								onPeopleCountClick(item);
								break;
							default:
								break;
							}
						}
					}).dialogList(mMoreOptionItemList).cancelable(true).build().show();
			return false;
		}
	};

	/**
	 * 点击进入点餐主页
	 *
	 * @param roomInfo
	 */
	private void onOrderClick(RoomInfo roomInfo)
	{
		Intent intent = new Intent(mContext, OrderMainActivity.class);
		intent.putExtra(IntentKey.ROOM_NUM, roomInfo.getRoomNum());
		intent.putExtra(IntentKey.ROOM_ID, roomInfo.getRoomId());
		intent.putExtra(IntentKey.BILL_ID, roomInfo.getBillId());
		intent.putExtra(IntentKey.ROOM_STATUS, roomInfo.getRoomStatus());
		intent.putExtra(IntentKey.ROOM_TYPE, roomInfo.getType());
		intent.putExtra(IntentKey.CHARGING_ROOM_TYPE_ID, roomInfo.getChargingRoomTypeId());
		mContext.startActivity(intent);
	}

	/**
	 * 点击修改人数
	 *
	 * @param roomInfo 房间信息
	 */
	private void onPeopleCountClick(RoomInfo roomInfo)
	{
		Intent intent1 = new Intent(mContext, SingleInputActivity.class);
		intent1.putExtra(IntentKey.CUSTOM_DIALOG_TITLE, "修改人数(" + roomInfo.getRoomNum() + ")");
		intent1.putExtra(IntentKey.CUSTOM_DIALOG_CONTENT, String.valueOf(roomInfo.getRoomPeopleCount()));
		intent1.putExtra(IntentKey.BILL_ID, roomInfo.getBillId());
		intent1.putExtra(IntentKey.BTN_SURE, "确定");
		intent1.putExtra(IntentKey.BTN_CANCEL, "取消");
		intent1.putExtra(IntentKey.ACTIVITY_ENUM, SingleInputActivity.ActivityEnum.ROOM_NUMBERS);
		((OrderHomePageActivity) mContext).startActivityForResult(intent1, IntentCode.ROOM_PEOPLE_REQUEST);
	}

	/**
	 * 点击修改备注
	 *
	 * @param roomInfo 房间信息
	 */
	private void onRemarkClick(RoomInfo roomInfo)
	{
		Intent intent = new Intent(mContext, CustomDialogActivity.class);
		intent.putExtra(IntentKey.CUSTOM_DIALOG_TITLE, "房间备注(" + roomInfo.getRoomNum() + ")");
		intent.putExtra(IntentKey.CUSTOM_DIALOG_TYPE, CustomDialogActivity.DialogEnum.REMARK.toString());
		intent.putExtra(IntentKey.CUSTOM_DIALOG_CONTENT, roomInfo.getRoomRemark());
		intent.putExtra(IntentKey.BILL_ID, roomInfo.getBillId());
		intent.putExtra(IntentKey.CUSTOM_DIALOG_CONTENT_HINT, "输入房间备注内容");
		((OrderHomePageActivity) mContext).startActivityForResult(intent, IntentCode.ROOM_REMARK_REQUEST);
	}

	/**
	 * 点击消费查询
	 *
	 * @param roomNum 房间号
	 * @param billId  账单id
	 */
	private void onConsumeQueryClick(String roomNum, String billId)
	{
		Intent intent = new Intent(mContext, ConsumeInfoActivity.class);
		intent.putExtra(IntentKey.ROOM_NUM, roomNum);
		intent.putExtra(IntentKey.BILL_ID, billId);
		mContext.startActivity(intent);
	}

	/**
	 * 房间状态为支付点餐
	 */
	public void roomStatusPaiedOrder(final RoomInfo roomInfo)
	{
		new VodDialog.Builder(mContext).dialogEnum(VodDialog.DialogEnum.DOUBLE).btnNegativeText(R.string.cancel).btnNeutralText(R.string.sure).cancelable(true)
				.contentName("点餐后会计算房费，确定续餐？").onCustomDialogListener(new VodDialog.Builder.OnCustomDialogListener()
				{
					@Override
					public void onOnlyCancelClick()
					{

					}

					@Override
					public void onOkClick()
					{
						onOrderClick(roomInfo);
					}

					@Override
					public void onCancelClick()
					{

					}
				}).build().show();
	}

	/**
	 * 动画开启闪烁
	 *
	 * @param view getView的view
	 */
	private void startFlick(View view)
	{
		if (null == view)
			return;
		if (null == alphaAnimation)
		{
			alphaAnimation = new AlphaAnimation(1, 0.2f);
			alphaAnimation.setDuration(500);
			alphaAnimation.setInterpolator(new LinearInterpolator());
			alphaAnimation.setRepeatCount(Animation.INFINITE);
			alphaAnimation.setRepeatMode(Animation.REVERSE);
		}
		view.startAnimation(alphaAnimation);
	}

	/**
	 * 清除闪烁动画
	 *
	 * @param view getView的view
	 */
	private void stopFlick(View view)
	{
		if (null == view)
			return;
		view.clearAnimation();
	}

	/**
	 * 清除所有闪烁效果
	 */
	public void clearAllAnimation()
	{
		if (sparseArray.size() > 0)
		{
			int size = sparseArray.size();
			for (int i = 0; i < size; i++)
			{
				View view = sparseArray.get(sparseArray.keyAt(i));
				stopFlick(view);
			}
			sparseArray.clear();
		}
	}

}
